package com.pheruagor.handmanager.storage.impl.sqlite {
    import com.pheruagor.handmanager.model.business.Club;
    import com.pheruagor.handmanager.storage.api.ClubStorage;

    import flash.data.SQLResult;
    import flash.data.SQLStatement;
    import flash.errors.SQLError;
    import flash.events.SQLEvent;

    public class ClubSQLiteStorage implements ClubStorage {
        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------
        private var m_table:Table;
        private var m_storage:SQLiteStorage;

        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------

        public function ClubSQLiteStorage() {
            createTable();
            m_storage = new SQLiteStorage();
        }

        private function get table():Table {
            return m_table;
        }

        private function createTable():void {
            m_table = new Table('club');
            m_table.creationStatement = "( id TEXT PRIMARY KEY, name TEXT, creationDate TEXT)";
        }


        public function saveClub(club:Club, onResult:Function, onFault:Function):void {
            if (club == null) {
                throw new ArgumentError("club cannot be null");
            }
            if (club.id == null) {
                throw new ArgumentError("club id cannot be null");
            }
            if (club.name == null || club.name.length == 0) {
                throw new ArgumentError("club name cannot be null or empty");
            }

            var name:String = club.name;
            var creationDate:String = club.creationDate == null ? null : club.creationDate.toString();
            table.insertStatement = "(id, name, creationDate) values('" + club.id + "','" + name + "','" + creationDate + "');";
            var resultHandler:Function = function(event:SQLEvent):void {
                    if (onResult != null) {
                        onResult(event);
                    }
                }
            var faultHandler:Function = function():void {
                    if (onFault != null) {
                        onFault();
                    }
                }
            m_storage.save(table, resultHandler, faultHandler);
        }

        public function listClubs(onResult:Function, onFault:Function):void {
            var resultHandler:Function = function(event:SQLEvent):void {
                    if (onResult != null) {
                        var sqlResult:SQLResult = SQLStatement(event.currentTarget).getResult();
                        var localResults:Array = sqlResult.data;
                        var results:Array = new Array();
                        for each (var item:Object in localResults) {
                            var club:Club = Club.fromObject(item);
                            results.push(club);
                        }
                        onResult(results);
                    }
                }
            var faultHandler:Function = function():void {
                    if (onFault != null) {
                        onFault();
                    }
                }

            table.selectStatement = "* FROM " + table.name + ";";
            m_storage.load(table, resultHandler, faultHandler);
        }

        public function removeClub(club:Club, onResult:Function = null, onFault:Function = null):void {
            if (club == null) {
                throw new ArgumentError("club cannot be null");
            }
            if (club.id == null) {
                throw new ArgumentError("club id cannot be null");
            }
            var resultHandler:Function = function(event:SQLEvent):void {
                    if (onResult != null) {
                        onResult();
                    }
                }
            var faultHandler:Function = function(event:SQLError):void {
                    if (onFault != null) {
                        onFault();
                    }
                }
            // remove all member of the club to removed
            var memberStorage:MemberSQLiteStorage = new MemberSQLiteStorage();
            memberStorage.removeAllMembersFromClub(club.id)

            table.removeStatement = "WHERE id='" + club.id + "';";
            m_storage.remove(table, resultHandler, faultHandler);
        }

        public function removeClubs(clubList:Array = null, onResult:Function = null, onFault:Function = null):void {
            table.removeStatement = ";";
            var resultHandler:Function = function(event:SQLEvent):void {
                    if (onResult != null) {
                        onResult();
                    }
                }
            var faultHandler:Function = function():void {
                    if (onFault != null) {
                        onFault();
                    }
                }

            if (clubList != null) {
                var memberStorage:MemberSQLiteStorage = new MemberSQLiteStorage();
                for each (var club:Club in clubList) {
                    memberStorage.removeAllMembersFromClub(club.id);
                }
            }
            m_storage.remove(table, resultHandler, faultHandler);
        }
    }

}